// (c)2016 Flipboard Inc, All Rights Reserved.

package com.vxb.testmvp.test_zonghe.network;


import com.vxb.testmvp.test_zonghe.network.api.LoginApi;
import com.vxb.testmvp.test_zonghe.network.api.V2Api;
import com.vxb.testmvp.test_zonghe.network.api.V3Api;
import com.vxb.testmvp.test_zonghe.network.api.ZhuangApi;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class NetworkManager {
    private static ZhuangApi zhuangApi;
    private static OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC)).build();
    private static Converter.Factory gsonConverterFactory = GsonConverterFactory.create();
    private static CallAdapter.Factory rxJavaCallAdapterFactory = RxJavaCallAdapterFactory.create();
    private static V3Api mV3APIService;
    private static V2Api v2Service;


    public static ZhuangApi getZhuangbiApi() {
        if (zhuangApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl("http://www.zhuangbi.info/")
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            zhuangApi = retrofit.create(ZhuangApi.class);
        }
        return zhuangApi;
    }

    public static V3Api V3Service() {
        Retrofit retrofitV3 = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl("http://192.168.0.240:8080/mpm_operate/api/v3/")
                .addConverterFactory(gsonConverterFactory)
                .addCallAdapterFactory(rxJavaCallAdapterFactory)
                .build();
        mV3APIService = retrofitV3.create(V3Api.class);
        return mV3APIService;
    }

    public static V2Api V2Service() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.0.240:8080/mpm_operate/api/v2/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(okHttpClient)
                .build();
        v2Service = retrofit.create(V2Api.class);
        return v2Service;
    }

    public static LoginApi LoginApi() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.0.240:8080/mpm_operate/api/v2/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(okHttpClient)
                .build();
        return retrofit.create(LoginApi.class);
    }

}
